package com.deluxelx.soul.inn.web.search;

import cn.hutool.core.collection.CollectionUtil;
import com.deluxelx.soul.inn.bean.article.ArticleVO;
import com.deluxelx.soul.inn.bean.counselor.CounselorVO;
import com.deluxelx.soul.inn.bean.course.CourseVO;
import com.deluxelx.soul.inn.bean.question.QuestionVO;
import com.deluxelx.soul.inn.bean.search.History;
import com.deluxelx.soul.inn.common.result.SoulInnResult;
import com.deluxelx.soul.inn.service.article.ArticleService;
import com.deluxelx.soul.inn.service.counselor.CounselorService;
import com.deluxelx.soul.inn.service.course.CourseService;
import com.deluxelx.soul.inn.service.question.QuestionService;
import com.deluxelx.soul.inn.service.search.HistoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 搜索框-Controller类
 *
 * @author liaozihao
 * @date 2024-04-18 9:29
 */
@RestController
@RequestMapping("/soul/inn/search")
public class SearchController {

    @Resource
    private HistoryService historyService;

    @Resource
    private ArticleService articleService;

    @Resource
    private CourseService courseService;

    @Resource
    private CounselorService counselorService;

    @Resource
    private QuestionService questionService;

    @GetMapping("/history")
    public SoulInnResult<List<History>> getSearchHistory(@RequestParam("userId") String userId) {
        List<History> historyList = historyService.fetchHistoryByUser(userId);
        if (CollectionUtil.isNotEmpty(historyList)) {
            return SoulInnResult.success(historyList);
        }
        return SoulInnResult.fail("获取搜索历史记录为空");
    }

    @PostMapping("/addHistory")
    public SoulInnResult<Boolean> addHistory(@RequestBody History history) {
        Boolean isSuccess = historyService.addHistory(history);
        if (isSuccess) {
            return SoulInnResult.success(Boolean.TRUE);
        }
        return SoulInnResult.fail("保存搜索历史失败");
    }

    @GetMapping("/deleteHistory")
    public SoulInnResult<Boolean> deleteHistory(@RequestParam("userId") String userId) {
        Boolean isSuccess = historyService.deleteHistory(userId);
        if (isSuccess) {
            return SoulInnResult.success(Boolean.TRUE);
        }
        return SoulInnResult.fail("删除搜索历史失败");
    }

    @GetMapping("/articleList")
    public SoulInnResult<List<ArticleVO>> fetchSearchArticleList(@RequestParam("keyword") String keyword) {
        List<ArticleVO> articleVOList = articleService.fetchSearchArticleList(keyword);
        if (CollectionUtil.isNotEmpty(articleVOList)) {
            return SoulInnResult.success(articleVOList);
        }
        return SoulInnResult.fail("搜索文章列表失败");
    }

    @GetMapping("/courseList")
    public SoulInnResult<List<CourseVO>> fetchSearchCourseList(@RequestParam("keyword") String keyword) {
        List<CourseVO> courseVOS = courseService.fetchSearchCourseList(keyword);
        if (CollectionUtil.isNotEmpty(courseVOS)) {
            return SoulInnResult.success(courseVOS);
        }
        return SoulInnResult.fail("搜索课程列表失败");
    }

    @GetMapping("/counselorList")
    public SoulInnResult<List<CounselorVO>> fetchSearchCounselorList(@RequestParam("keyword") String keyword) {
        List<CounselorVO> counselorVOS = counselorService.fetchSearchCounselorList(keyword);
        if (CollectionUtil.isNotEmpty(counselorVOS)) {
            return SoulInnResult.success(counselorVOS);
        }
        return SoulInnResult.fail("搜索咨询师列表失败");
    }

    @GetMapping("/questionList")
    public SoulInnResult<List<QuestionVO>> fetchSearchQuestionList(@RequestParam("keyword") String keyword) {
        List<QuestionVO> questionVOList = questionService.fetchSearchQuestionList(keyword);
        if (CollectionUtil.isNotEmpty(questionVOList)) {
            return SoulInnResult.success(questionVOList);
        }
        return SoulInnResult.fail("搜索心理问答列表失败");
    }

}
